//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.ComponentModel;
using System.Drawing;
using System.Workflow.ComponentModel;
using Microsoft.Research.DataLayer;
using TridentContext;

namespace Microsoft.Research.ScientificWorkflow.Activities
{
    [Name("Data Product Reader")]
    [Description("Reads the data from the data product from the registry.")]
    [WorkflowCategory("Data")]
    public class DataProductReaderActivity : System.Workflow.ComponentModel.Activity
    {
        private Connection registryConnection;

        #region Dependency Properties

        public static DependencyProperty DataProdIdProperty = DependencyProperty.Register("DataProdId",
            typeof(string), typeof(DataProductReaderActivity));

        [RequiredInputParam]
        [Name("DataProduct Id")]
        [Description("String representing the GUID of the data product.")]
        public string DataProdId
        {
            get { return ((string)(base.GetValue(DataProductReaderActivity.DataProdIdProperty))); }
            set { base.SetValue(DataProductReaderActivity.DataProdIdProperty, value); }
        }

        public static DependencyProperty ParamTypeProperty = DependencyProperty.Register("ParamType",
            typeof(string), typeof(DataProductReaderActivity));

        [OutputParam]
        [Name("DataProduct Type")]
        [Description("String representing the type of the data product.")]
        public string ParamType
        {
            get { return ((string)(base.GetValue(DataProductReaderActivity.ParamTypeProperty))); }
            set { base.SetValue(DataProductReaderActivity.ParamTypeProperty, value); }
        }

        public static DependencyProperty StringValueProperty = DependencyProperty.Register("StringValue",
            typeof(string), typeof(DataProductReaderActivity));

        [OutputParam]
        [Name("String Value")]
        [Description("String value of the data product.")]
        public string StringValue
        {
            get { return ((string)(base.GetValue(DataProductReaderActivity.StringValueProperty))); }
            set { base.SetValue(DataProductReaderActivity.StringValueProperty, value); }
        }

        public static DependencyProperty IntegerValueProperty = DependencyProperty.Register("IntegerValue",
            typeof(int), typeof(DataProductReaderActivity));

        [OutputParam]
        [Name("Integer Value")]
        [Description("Integer value of the data product.")]
        public int IntegerValue
        {
            get { return ((int)(base.GetValue(DataProductReaderActivity.IntegerValueProperty))); }
            set { base.SetValue(DataProductReaderActivity.IntegerValueProperty, value); }
        }

        public static DependencyProperty ImageValueProperty = DependencyProperty.Register("ImageValue",
            typeof(Image), typeof(DataProductReaderActivity));

        [OutputParam]
        [Name("Image Value")]
        [Description("Integer value of the data product.")]
        public Image ImageValue
        {
            get { return ((Image)(base.GetValue(DataProductReaderActivity.ImageValueProperty))); }
            set { base.SetValue(DataProductReaderActivity.ImageValueProperty, value); }
        }

        public static DependencyProperty ByteArrayValueProperty = DependencyProperty.Register("ByteArrayValue",
            typeof(byte[]), typeof(DataProductReaderActivity));

        [OutputParam]
        [Name("Byte Array Value")]
        [Description("Byte Array value of the data product.")]
        public byte[] ByteArrayValue
        {
            get { return ((byte[])(base.GetValue(DataProductReaderActivity.ByteArrayValueProperty))); }
            set { base.SetValue(DataProductReaderActivity.ByteArrayValueProperty, value); }
        }

        #endregion

        #region Over Ridden Methods

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            Context ctx = Context.Get();
            if (null == ctx)
            {
                return ActivityExecutionStatus.Faulting;
            }

            this.registryConnection = ctx.Connection.Clone();
            this.registryConnection.Open();

            DataProduct dp = GetDataProduct(new Guid(this.DataProdId));
            if (null != dp)
            {
                GetDPValue(ref dp);
            }

            // Close the registry Connection.
            if (this.registryConnection != null)
            {
                this.registryConnection.Close();
                this.registryConnection = null;
            }

            return ActivityExecutionStatus.Closed;
        }
        
        #endregion

        #region Private Methods 

        private DataProduct GetDataProduct(Guid dataProdId)
        {
            return DataProduct.Load(dataProdId, this.registryConnection);
        }

        private void GetDPValue(ref DataProduct dp)
        {
            if ((0 == dp.Type.ToLower().CompareTo("text/richtext")) ||
                   (0 == dp.Type.ToLower().CompareTo("text/plain")) ||
                   (0 == dp.Type.ToLower().CompareTo("text/html")) ||
                   (0 == dp.Type.ToLower().CompareTo("text/xml")))
            {
                this.StringValue = dp.ContentsAsString;
                
            }
            else if ((0 == dp.Type.ToLower().CompareTo("image/gif"))
                    || (0 == dp.Type.ToLower().CompareTo("image/jpg"))
                    || (0 == dp.Type.ToLower().CompareTo("Image/bmp"))
                    || (0 == dp.Type.ToLower().CompareTo("image/png")))
            {
                this.ImageValue = dp.ContentsAsImage;
            }
            else 
            {
                this.ByteArrayValue = dp.Contents;
            }
            this.ParamType = dp.Type;
        }

        #endregion

    }
}
